package mr.ali.webvideo.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * HTTP客户端工具类
 */
@Slf4j
public class HttpClientUtil {

    // 连接超时时间，单位毫秒
    private static final int CONNECT_TIMEOUT = 5000;
    // 请求超时时间
    private static final int REQUEST_TIMEOUT = 5000;
    // 套接字超时时间
    private static final int SOCKET_TIMEOUT = 10000;

    /**
     * 发送GET请求
     *
     * @param url 请求URL
     * @return 响应内容
     */
    public static String doGet(String url) {
        return doGet(url, null);
    }

    /**
     * 发送GET请求
     *
     * @param url     请求URL
     * @param charset 字符集
     * @return 响应内容
     */
    public static String doGet(String url, String charset) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String result = null;

        try {
            HttpGet httpGet = new HttpGet(url);
            // 设置请求和传输超时时间
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(CONNECT_TIMEOUT)
                    .setConnectionRequestTimeout(REQUEST_TIMEOUT)
                    .setSocketTimeout(SOCKET_TIMEOUT)
                    .build();
            httpGet.setConfig(requestConfig);

            response = httpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                if (charset != null) {
                    result = EntityUtils.toString(entity, charset);
                } else {
                    result = EntityUtils.toString(entity, StandardCharsets.UTF_8);
                }
            }
        } catch (Exception e) {
            log.error("发送GET请求失败", e);
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                httpClient.close();
            } catch (IOException e) {
                log.error("关闭HTTP连接失败", e);
            }
        }
        return result;
    }

    /**
     * 发送POST请求
     *
     * @param url  请求URL
     * @param data 请求数据
     * @return 响应内容
     */
    public static String doPost(String url, String data) {
        return doPost(url, data, null);
    }

    /**
     * 发送POST请求
     *
     * @param url     请求URL
     * @param data    请求数据
     * @param charset 字符集
     * @return 响应内容
     */
    public static String doPost(String url, String data, String charset) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String result = null;

        try {
            HttpPost httpPost = new HttpPost(url);
            // 设置请求和传输超时时间
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(CONNECT_TIMEOUT)
                    .setConnectionRequestTimeout(REQUEST_TIMEOUT)
                    .setSocketTimeout(SOCKET_TIMEOUT)
                    .build();
            httpPost.setConfig(requestConfig);

            // 设置请求体
            if (data != null) {
                StringEntity entity = new StringEntity(data, charset != null ? charset : "UTF-8");
                entity.setContentType("application/xml");
                httpPost.setEntity(entity);
            }

            response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                if (charset != null) {
                    result = EntityUtils.toString(entity, charset);
                } else {
                    result = EntityUtils.toString(entity, StandardCharsets.UTF_8);
                }
            }
        } catch (Exception e) {
            log.error("发送POST请求失败", e);
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                httpClient.close();
            } catch (IOException e) {
                log.error("关闭HTTP连接失败", e);
            }
        }
        return result;
    }
}
